package com.tct.worktimeboard.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tct.worktimeboard.entity.ProjectType;
import com.tct.worktimeboard.service.IProjectTypeService;
import com.tct.worktimeboard.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyongkui
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/project-type")
public class ProjectTypeController {
    @Autowired
    private IProjectTypeService projectTypeService;

    @GetMapping("/listPage")
    public JSONObject listPageProjectType(ProjectType projectType){
        return projectTypeService.listPageProjectType(projectType);
    }

    @PostMapping("/set")
    public JSONObject set(@RequestBody ProjectType projectType){
        int count = projectTypeService.count(new QueryWrapper<ProjectType>().eq("type_name", projectType.getTypeName()));
        if(count > 0){
            return JsonUtil.toErrorData("项目分类已存在");
        }
        boolean flag = projectTypeService.save(projectType);
        if(flag){
            return JsonUtil.toJsonData();
        }
        return JsonUtil.toErrorData("添加失败");
    }

    @PutMapping("/edit")
    public JSONObject edit(@RequestBody ProjectType projectType){
        boolean flag = projectTypeService.updateById(projectType);
        if(flag){
            return JsonUtil.toJsonData();
        }
        return JsonUtil.toErrorData("修改失败");
    }

    @DeleteMapping("/del")
    public JSONObject del(Integer... ids){
        boolean flag = projectTypeService.removeByIds(Arrays.asList(ids));
        if(flag){
            return JsonUtil.toJsonData();
        }
        return JsonUtil.toErrorData("删除失败");
    }
}
